package com.xywl.escort.view.order.delivery;

import com.xywl.escort.bean.BaseReq;
import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.DeliveryOrderResp;
import com.xywl.escort.bean.ProjectLogisticsResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class DeliveryOrderPresenter implements DeliveryOrderContract.Presenter {
    private DeliveryOrderContract.View mView;
    private ApiServer mApiServer;

    @Inject
    DeliveryOrderPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(DeliveryOrderContract.View view) {
        this.mView = view;
    }

    @Override
    public void loadOrderList(int pageNum) {
        BaseReq req = new BaseReq();
        req.pageNum = pageNum;
        mApiServer.deliveryOrderList(req)
                .compose(RxUtil.<DeliveryOrderResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<DeliveryOrderResp>bindToLife())
                .subscribe(new BaseSubscriber<DeliveryOrderResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadOrderListFailure();
                    }

                    @Override
                    public void onNext(DeliveryOrderResp resp) {
                        if (null != resp && null != resp.data && resp.isOk()) {
                            mView.loadOrderListSuccess(resp.data);
                        } else {
                            mView.loadOrderListFailure();
                        }
                    }
                });
    }

    @Override
    public void delOrder(String id, final int position) {
        BaseReq req = new BaseReq();
        req.id = id;
        mApiServer.deliveryDelOrder(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.delOrderFailure("操作失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.delOrderSuccess(resp, position);
                            } else {
                                mView.delOrderFailure(resp.msg);
                            }
                        } else {
                            mView.delOrderFailure("操作失败");
                        }
                    }
                });
    }

    @Override
    public void remindOrder(String id) {
        BaseReq req = new BaseReq();
        req.id = id;
        mApiServer.deliveryRemindOrder(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.remindOrderFailure("操作失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.remindOrderSuccess(resp);
                            } else {
                                mView.remindOrderFailure(resp.msg);
                            }
                        } else {
                            mView.remindOrderFailure("操作失败");
                        }
                    }
                });
    }

    @Override
    public void confirmReceipt(String id) {
        BaseReq req = new BaseReq();
        req.id = id;
        mApiServer.deliveryConfirm(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.confirmReceiptFailure("操作失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.confirmReceiptSuccess(resp);
                            } else {
                                mView.confirmReceiptFailure(resp.msg);
                            }
                        } else {
                            mView.confirmReceiptFailure("操作失败");
                        }
                    }
                });
    }

    @Override
    public void logisticsOrder(String id) {
        BaseReq req = new BaseReq();
        req.id = id;
        mApiServer.deliveryLogisticsOrder(req)
                .compose(RxUtil.<ProjectLogisticsResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<ProjectLogisticsResp>bindToLife())
                .subscribe(new BaseSubscriber<ProjectLogisticsResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.logisticsOrderFailure("操作失败");
                    }

                    @Override
                    public void onNext(ProjectLogisticsResp resp) {
                        if (null != resp) {
                            if (null != resp.data && resp.isOk()) {
                                mView.logisticsOrderSuccess(resp.data);
                            } else {
                                mView.logisticsOrderFailure(resp.msg);
                            }
                        } else {
                            mView.logisticsOrderFailure("操作失败");
                        }
                    }
                });
    }

    @Override
    public void unSubscribe() {
        Timber.tag("DeliveryOrderPresenter").e("unSubscribe");
        mView = null;
    }
}
